﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace RouterUtimeChecker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        short MinutesLeft = 60;
        short CheckInterval = 60;
        string URL = "http://192.168.1.254/cgi/b/is/_ethoa_/ov/?be=0&l0=2&l1=2&name=Internet";

        string OnlineTime = "";
        string SendData = "";
        string currentIp = "";

        private void CheckTimer_Tick(object sender, EventArgs e)
        {
            MinutesLeft--;

            if (MinutesLeft == 0)
            {
                CheckRouterState();
                MinutesLeft = CheckInterval;
            }

            label1.Text = MinutesLeft.ToString();
        }

        private void CheckRouterState()
        {
            String RouterHTML = GetRouterHTTPString();
            RouterHTML = RouterHTML.Remove(0, 5000);
            int CharPointer;

            //Remove to Online Time
            CharPointer = RouterHTML.IndexOf("Aktiv tid:") + 31;
            RouterHTML = RouterHTML.Remove(0, CharPointer);

            //Get Online time
            CharPointer = RouterHTML.IndexOf("</td></tr>");
            OnlineTime = RouterHTML.Substring(0, CharPointer);


            //Remove to SentData
            CharPointer = RouterHTML.IndexOf("Överförda data") + 64;
            RouterHTML = RouterHTML.Remove(0, CharPointer);
            
            //get the SentData
            CharPointer = RouterHTML.IndexOf("</td></tr>");
            SendData = RouterHTML.Substring(0, CharPointer);


            //Remove to ip
            CharPointer = RouterHTML.IndexOf("IP-adress:") + 32;
            RouterHTML = RouterHTML.Remove(0, CharPointer);

            //get the Ip
            CharPointer = RouterHTML.IndexOf("</td></tr>");
            currentIp = RouterHTML.Substring(0, CharPointer);

            

            ListViewItem Item1 = new ListViewItem();

            Item1.Text = GetRouterRestartDate(OnlineTime);
            Item1.SubItems.Add(OnlineTime);
            Item1.SubItems.Add(currentIp);
            Item1.SubItems.Add(SendData);
            Item1.SubItems.Add(DateTime.Now.ToString("d-H:m:s"));

            listView1.Items.Add(Item1);

            writeToFile(DateTime.Now.ToString("d-H:m:s")+ 
                        ": - Last Reboot: " + OnlineTime + 
                        " Ip: " + currentIp + 
                        " Trafic: " + SendData); 

            return;
        }

        private string GetRouterHTTPString()
        {
            try
            {

            }
            catch (Exception e)
            {
                
            }

            // Open a connection
            HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(URL);

            // You can also specify additional header values like 
            // the user agent or the referer:
            WebRequestObject.UserAgent = ".NET Framework/3.5";

            // Request response:
            WebResponse Response = WebRequestObject.GetResponse();

            // Open data stream:
            Stream WebStream = Response.GetResponseStream();

            // Create reader object:
            StreamReader Reader = new StreamReader(WebStream);

            // Read the entire stream content:
            string PageContent = Reader.ReadToEnd();

            // Cleanup
            Reader.Close();
            WebStream.Close();
            Response.Close();

            return PageContent;
        }

        private string GetRouterRestartDate(string inDate)
        {
            int CharPointer;
            string date = inDate;
            int days;
            int hours;
            int minutes;
            int seconds;
            

            CharPointer = date.IndexOf(" ");
            days = int.Parse(date.Substring(0,CharPointer));

            CharPointer = date.IndexOf(",") + 2;
            date = date.Remove(0,CharPointer);

            CharPointer = date.IndexOf(":");
            hours = int.Parse(date.Substring(0, CharPointer));

            date = date.Remove(0, CharPointer + 1);

            CharPointer = date.IndexOf(":");
            minutes = int.Parse(date.Substring(0, CharPointer));

            date = date.Remove(0, CharPointer + 1);

            seconds = int.Parse(date);


            TimeSpan RestartDate = new TimeSpan(days, hours, minutes, seconds);
            return DateTime.Now.Subtract(RestartDate).ToString("d-H:m:s");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            CheckRouterState();
            label1.Text = MinutesLeft.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckRouterState();
        }

        private void writeToFile(string msg)
        {
            TextWriter FileWriter = new StreamWriter("Log.txt", true);
            FileWriter.WriteLine(msg);
            FileWriter.Flush();
            FileWriter.Close();
        }
    }
}
